﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Common;
using Common.Config;
using Common.Model;
using ConsoleService.API;
using ConsoleService.Model;
using HtmlAgilityPack;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using NLog;
using NLog.Web;
using Quartz;


namespace ConsoleService.Job
{
    /// <summary>
    /// 同步数据，并实现Excute方法
    /// </summary>
    public class UpdateCompanyJob : IJob
    {
        Logger logger = LogManager.GetCurrentClassLogger();
        public UpdateCompanyJob()
        {
            NLog.Web.NLogBuilder.ConfigureNLog("nlog.config");
        }

        public Task Execute(IJobExecutionContext context)
        {
            return Task.Run(() =>
            {

                var triggerData = context.Trigger.JobDataMap;//获取Trigger中的参数
                var data = context.MergedJobDataMap;//获取Job和Trigger中合并的参数
                CommonAPI commonAPI = new CommonAPI();

                var s = new Stopwatch();
                s.Start();

                var pageindex = 1;
                var pageSize = 1000;//分页条数
                var result2 = commonAPI.GetCompanyList2(pageindex, pageSize);
                var records = result2.data.records;//总记录数
                var total = records % pageSize == 0 ? records / pageSize : records / pageSize + 1;//总页数
                var text = $"开始分页更新公司数据共计：{records}条数据，总页数：{total}....";

                Console.WriteLine(text);
                logger.Warn(text);

                for (int i = pageindex; i <= total; i++)
                {
                    Console.WriteLine($"-----------当前第{i}页：-------------");
                    var result = commonAPI.GetCompanyList2(i, pageSize);
                    var New_pageSize = 100;//分页条数
                    int j = New_pageSize;
                    for (int z = 0; z < result.data.rows.Count(); z += New_pageSize)
                    {
                        var list = result.data.rows.Take(j).Skip(z).ToList();
                        Task.Run(() =>
                        {
                            Update(list);
                        });
                        j += New_pageSize;
                    }
                }


                s.Stop();
                var msg = $"分页更新公司数据共计：{records}条数据，总耗时：{s.Elapsed.ToString()}。{DateTime.Now.ToString()}";

                logger.Warn(msg);
                Console.WriteLine(msg);



            });
        }


        public void Update(List<Company> list)
        {
            var result = new List<Company>();
            var commonAPI = new CommonAPI();
            var index = 1;
            foreach (var item in list)
            {
                var url = $"https://www.tianyancha.com/company/{item.tianyancha_Id}";
                var text = $"{index}.{item.name}-{url}";
                Console.WriteLine(text);
                logger.Warn(text);
                var entity = commonAPI.GetCompanyDetail(url);
                if (entity != null)
                {
                    result.Add(entity);
                }
                index++;
            }
            commonAPI.UpdateCompany(result);
        }
    }
}
